<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="150px" size="small">
    <el-form-item label="区域名称" prop="buildingName">
      <el-input v-model="dataForm.buildingName" placeholder="区域名称" maxlength="100"></el-input>
    </el-form-item>
      <el-form-item label="所属区域" prop="parentBuildingName">
        <!--<el-popover-->
          <!--ref="menuListPopover"-->
          <!--placement="bottom-start"-->
          <!--trigger="click">-->
          <!--<div style="height: 350px">-->
            <!--<el-scrollbar style="height: 100%;width: 100%">-->
              <!--<el-tree-->
                <!--:data="menuList"-->
                <!--:props="menuListTreeProps"-->
                <!--node-key="buildingId"-->
                <!--ref="menuListTree"-->
                <!--@current-change="menuListTreeCurrentChangeHandle"-->
                <!--:default-expand-all="true"-->
                <!--:highlight-current="true"-->
                <!--:expand-on-click-node="false">-->
              <!--</el-tree>-->
            <!--</el-scrollbar>-->
          <!--</div>-->
        <!--</el-popover>-->
        <el-input v-model="dataForm.parentBuildingName" v-popover:menuListPopover disabled :readonly="true" placeholder="点击选择所属区域" class="menu-list__input"></el-input>
      </el-form-item>
    <el-form-item label="区域类型">
      <el-select v-model="dataForm.buildingType">
        <el-option v-for="item in buildTypeList" :key="item.paramValue" :label="item.paramValue" :value="item.paramKey" />
      </el-select>
    </el-form-item>
    <el-form-item label="区域面积(平方米)" prop="buildingArea">
      <el-input v-model="dataForm.buildingArea" placeholder="区域面积(平方米)" maxlength="100"></el-input>
    </el-form-item>
    <el-form-item label="容纳人数" prop="buildingPersons">
      <el-input v-model="dataForm.buildingPersons" placeholder="容纳人数" maxlength="11"></el-input>
    </el-form-item>
    <el-form-item label="项目编号">
      <el-select v-model="dataForm.projectId" filterable>
        <el-option v-for="item in projectList" :key="item.projectId" :label="item.projectName+'['+item.projectId+']'" :value="item.projectId" />
      </el-select>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false" size="small">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" size="small">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { treeDataTranslate } from '@/utils'
  export default {
    data () {
      return {
        buildTypeList :[],
        projectList :[],
        visible: false,
        dataForm: {
          buildingId: '',
          buildingName: '',
          parentBuildingId: '-1',
          buildingType: '',
          buildingArea: '',
          buildingPersons: '',
          projectId: '',
          parentBuildingName:''
        },
        menuList: [],
        menuListTreeProps: {
          label: 'buildingName',
          children: 'children'
        },
        dataRule: {
          buildingName: [
            { required: true, message: '区域名称不能为空', trigger: 'blur' }
          ],
          parentBuildingId: [
            { required: true, message: '所属区域不能为空', trigger: 'blur' }
          ],
          buildingType: [
            { required: true, message: '区域类型不能为空', trigger: 'blur' }
          ],
          buildingArea: [
            { required: true, message: '区域面积不能为空', trigger: 'blur' }
          ],
          buildingPersons: [
            { required: true, message: '容纳人数不能为空', trigger: 'blur' }
          ],
          projectId: [
            { required: true, message: '项目编号不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id,buildTypeList,projectList,currentbuildInfo) {
        this.buildTypeList =buildTypeList || []
        this.projectList =projectList ||[]
        this.dataForm.buildingId = id || 0

        this.$api.okhttp({
          url: this.$http.adornUrl('/ecs/ecsbuildinginfo/select'),
          method: 'get',
          params: this.$http.adornParams({
            'buildingid': this.dataForm.buildingId,
            'projectid': this.$util.cookies.get("cuuren_projectId"),
            "customerId": this.$util.cookies.get('customer_id'),
          })
        }).then((data) => {
          this.menuList = treeDataTranslate(data.menuList, 'buildingId','parentBuildingId')
      }).then(() => {
          this.visible = true
         this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
      })
      }).then(() => {
          if (this.dataForm.buildingId) {
            this.$api.okhttp({
              url: this.$http.adornUrl(`/ecs/ecsbuildinginfo/info/${this.dataForm.buildingId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 0) {
                this.dataForm.buildingId = data.ecsbuildinginfo.buildingId
                this.dataForm.buildingName = data.ecsbuildinginfo.buildingName
                this.dataForm.parentBuildingId = data.ecsbuildinginfo.parentBuildingId
                this.dataForm.buildingType = data.ecsbuildinginfo.buildingType
                this.dataForm.buildingArea = data.ecsbuildinginfo.buildingArea
                this.dataForm.buildingPersons = data.ecsbuildinginfo.buildingPersons
                this.dataForm.projectId = data.ecsbuildinginfo.projectId
                 this.dataForm.parentBuildingName = data.ecsbuildinginfo.parentBuildingName
                 this.menuListTreeSetCurrentNode()
              }
            })
          }else{
          // 新增
          // this.menuListTreeSetCurrentNode()
          // this.dataForm.projectId =''
          // this.dataForm.buildingType =''
          // this.dataForm.parentBuildingName =''

            this.dataForm.parentBuildingId = currentbuildInfo.buildingId||-1
            this.dataForm.parentBuildingName = currentbuildInfo.buildingName
        }
        })
      },
      // 菜单树选中
      menuListTreeCurrentChangeHandle (data, node) {
        this.dataForm.parentBuildingId = data.buildingId
        this.dataForm.parentBuildingName = data.buildingName
      },
      // 菜单树设置当前选中节点
      menuListTreeSetCurrentNode () {
        this.$refs.menuListTree.setCurrentKey(this.dataForm.parentBuildingId)
        this.dataForm.parentBuildingName = (this.$refs.menuListTree.getCurrentNode() || {})['buildingName']
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$api.okhttp({
              url: this.$http.adornUrl(`/ecs/ecsbuildinginfo/${!this.dataForm.buildingId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'buildingId': this.dataForm.buildingId || undefined,
                'buildingName': this.dataForm.buildingName,
                'parentBuildingId': this.dataForm.parentBuildingId,
                'buildingType': this.dataForm.buildingType,
                'buildingArea': this.dataForm.buildingArea,
                'buildingPersons': this.dataForm.buildingPersons,
                'projectId': this.dataForm.projectId,
                "customerId": this.$util.cookies.get('customer_id'),
              })
            }).then((data) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
<style>
  el-form-item{
    margin-top: 20px;
  }
</style>
